App({
  onLaunch: function (options) {
    // 比较缓存是否过期
    this.checkLogin(res => {
      console.log('is_login: ', res.is_login)
      // if (res.is_login!='200') {
      //   wx.clearStorage()
      // }
    })
  },
  onShow: function (options) {

  },
  onHide: function () {

  },
  onError: function (msg) {

  },
  onPageNotFound: function (options) {

  },
  // 分享给微信好友
  onShareAppMessage: function () {
  },
  // 全局数据
  globalData: {
    userInfo:[{avatarUrl:'',nickName:''}],
    token:null,
  },

  checkLogin: function(callback) {
     var token = wx.getStorageSync('cookieToken') // 从数据缓存中获取token
      // if (token) {
        this.globalData.token = token;
        wx.request({
          url: 'https://www.fastmock.site/mock/b1f1aa4f57847f589326c5a55ea93a0e/comm/token',
          method: 'GET',
          header: {// 设置请求的 header
            'content-type': 'application/json',
            token:wx.getStorageSync('cookieToken')
          },
          success: res => {
            // console.log(res.data.data)
            if (res.data.code!='200') {
              wx.clearStorage()
              callback({ is_login: false })
            }else{
              wx.setStorageSync('cookieToken', res.data.data)
              wx.setStorageSync('cookieNickname', res.data.nickName)
            callback({ is_login: true })
            }
          }
      })
    // }
    // else {   
    //   callback({ is_login: false })
    //   return
    // }
  },
   //存入用户信息到本地缓存
setGlobalUserInfo(token,nickname,avatar){
  wx.setStorageSync('cookieToken', token),
  wx.setStorageSync('cookieNickname', nickname),
  wx.setStorageSync('cookieAvatar', avatar)
  },
  //从本地缓存取出用户信息
  getGlobalUserInfo(){
      return({
       "cookieToken":wx.getStorageSync('cookieToken'),
       "cookieNickname":wx.getStorageSync('cookieNickname'),
       "cookieAvatar":wx.getStorageSync('cookieAvatar'),
      })
    },
    // 设置全局URL
  getURL(){
    // return "http://42.193.247.154:8000/service"
    //没得了，先全部改为模拟静态的地址接口数据
    return "https://www.fastmock.site/mock/b1f1aa4f57847f589326c5a55ea93a0e/comm"
  }
});
